<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ajax</title>
<script type="text/javascript">
	var xmlHttp;
	function createXMLHttpRequest() {
	    try {
	         xmlHttp = new XMLHttpRequest();
	    } catch (tryMS) {
	        try {
	             xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	        } catch (otherMS) {
	            try {
	                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	           } catch (failed) {
	                 xmlHttp = null;
		        // 这里可以报一个错误，无法获得 XMLHttpRequest对象	
	           }
	        }
	     }
	 	return xmlHttp;
	}
	
	function loadJSON(){
		//1.创建XMLHttpRequest对象	
		//xmlHttp = createXMLHttpRequest();
		xmlHttp = new XMLHttpRequest();
		//2.将状态触发器绑定到一个参数
		xmlHttp.onreadystatechange=getJson;
		//3.使用open方法简历与服务器的连接
		xmlHttp.open("post","/dao1/jsonarray");    //这里注意url
		//4.向服务端发送数据
		xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
		xmlHttp.send(null);
	}
	
	//5.在回调函数中对返回的数据进行处理
	function getJson(){
		alert(xmlHttp.readyState + ":" + xmlHttp.status);
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200){
				var div = document.getElementById("myDiv");
//				div.innerHTML = xmlHttp.responseText;
				//将JSON字符串转化为JS对象
				var obj=eval("("+xmlHttp.responseText+")");
				var msg="<table><tr><th>用户姓名</th><th>用户密码</th></tr>";
				
				for(var i=0;i<obj.length;i++){
				msg+="<tr><td>"+obj[i].uname+"</td>";
				msg+="<td>"+obj[i].pwd+"</td>";
				msg+="</tr>";
				}
				msg+="</table>";
				div.innerHTML = msg;
			}else if(xmlHttp.status==404){
				
			}else if(xmlHttp.status==500){
				
			}
		}
	}
</script>

</head>
<body>
	<div id="myDiv"><h3>Let AJAX change this text</h3></div>
	<button type="button" onclick="loadJSON()">Change Content</button>
</body>
</html>